<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/28
 * Time: 11:33
 */

namespace Sms\Lib;


class ImiLib
{
    /* 发送短信 */
    public function sms_send($mobile){
        //step1:验证码 和 手机号
        $sms_recNum  = $mobile;  		//手机号
        $rand_number = randNum(6);  // 验证码

        //step2:把 手机号和验证码 存入cookie中
        session('verificationMobile',$sms_recNum);	//手机号cookie
        session('verificationCode',$rand_number);	//验证码cookie

        //step3:短信安全配置 短信配置 及 发送
        import("AliMsgSend");
        $appKey = C('APPKEY');
        $secreKey = C('SECRETKEY');
        $re = new \AliMsgSend($appKey, $secreKey);

        $recNum = $sms_recNum; 														// 要发送的手机号码，注意这个是字符串类型的
        $smsParam = "{code:'".$rand_number."',product:'".C('SMS_PRODUCT')."'}"; 	// 模板变量
        $smsTemplateCode = C('SMS_TEMPLATECODE'); 									// 模板id
        $smsFreeSignName = C('SMS_SIGNNAME'); 										// 签名
        $res =$re->sendMsg($recNum, $smsParam, $smsTemplateCode, $smsFreeSignName);

        //step5:状态及输出，这里有问题
        if($res){
            $arr['status'] = '1';
            $arr['msg']='发送成功，验证码不要告诉其他人！';
        }else{
            $arr['status'] = '0';
            $arr['msg'] = '发送失败，可能你的账户存在问题！';  //这里只判断0和1两种状态，1 表示成功，0 可能是账户未开通/短信超过数量/加入黑名单 等等,根据籽官方手册查询
        }
        return $arr;
    }


    /* 短信验证码校验 */
    public function verification($code,$mobile){

        //step1:cookie中取出验证码手机号
        $verificationCode = session('verificationCode');
        $verificationMobile = session('verificationMobile');

        //step2:用户传递验证码
        $verificationCodeCorr = $code;
        $verificationMobileCorr = $mobile;

        //step3:验证手机号和验证码状态及输出
        if($verificationCode == $verificationCodeCorr && $verificationMobile==$verificationMobileCorr){
            $arr['status'] = '1';
            $arr['msg'] = '验证码校验成功';
        } else {
            $arr['status'] = '0';
            $arr['msg'] = '验证码校验失败';
        }

        return $arr;
    }
}